<?php

namespace app\api\controller;
//导入基类
use app\common\model\Coupon;
use app\common\model\Shoporder;
use app\common\model\UserCoupon as user_couponModel;
use app\common\model\User;
use think\Controller;
use think\facade\Cache;
use think\Db;
use app\common\model\Banner;
use app\common\model\Goods as goodsModel;
use think\Request;
use Endroid\QrCode\QrCode;
use app\common\model\IntegralLog as integralLoglModel;
use app\common\model\WalletLog as walletLoglModel;
use app\common\model\GoodsCategory as GoodsCategoryModel;
class GoodsOrder extends Controller
{
    //我的订单
    public function index()
    {
        $userid = session('userid');
        $status = $this->request->param('status') ? $this->request->param('status') : 1;
        $start = 0;
        $limit = 10;
        if (!empty($param['length']) && !empty($param['start'])) {
            $start = ($param['start'] - 1) * $param['length'];
            $limit = $param['length'];
        }
        $list = Shoporder::alias('s')->join('langjin_goods g', 's.goods_id = g.id')->where('s.userid', $userid)->where('status', $status)->field('s.order_id,s.tprice,s.status,s.create_time,g.name,g.play_start_time,g.play_end_time')->limit($start, $limit)->select();
        foreach ($list as $key => $value) {
            $list[$key]['play_start_time'] = date('Y-m-d', $list[$key]['play_start_time']);
            $list[$key]['play_end_time'] = date('Y-m-d', $list[$key]['play_end_time']);
        }
        $count = Shoporder::alias('s')->join('langjin_goods g', 's.goods_id = g.id')->where('s.userid', $userid)->where('status', $status)->count();
        $data = array('code' => 0, 'msg' => '获取成功', 'info' => $list, 'count' => $count);
        return json($data);
    }

    //取消订单
    public function cancel_order()
    {
        $data = array('code' => 1, 'msg' => '数据异常');
        if (request()->isPost()) {
            $userid = session('userid');
            $order_id = $this->request->param('order_id');
            $reason = $this->request->param('reason');
            $info = Shoporder::where(['order_id' => $order_id, 'userid' => $userid])->find();
            if ($info) {
                //未付款订单取消订单
                if ($info['status'] == 0) {
                    if (empty($reason)) {
                        $data = array('code' => 1, 'msg' => '取消原因不能为空');
                        return json($data);
                    }
                    $res = Shoporder::where(['order_id' => $order_id, 'userid' => $userid])->update(['status' => 3, 'reason' => $reason]);
                    if ($res) {
                        $data = array('code' => 0, 'msg' => '订单取消成功');
                        return json($data);
                    }
                } elseif ($info['status'] == 1) {
                    //已付款订单取消订单
                    if (empty($reason)) {
                        $data = array('code' => 1, 'msg' => '取消原因不能为空');
                        return json($data);
                    }
                    $res = Shoporder::where(['order_id' => $order_id, 'userid' => $userid])->update(['status' => 2, 'reason' => $reason]);
                   $res1 =  goodsModel::where('id',$info['goods_id'])->setInc('amount',1);
                    if ($res && $res1) {
                        $data = array('code' => 0, 'msg' => '订单取消成功');
                        return json($data);
                    }
                } else {
                    return json($data);
                }
            }
        }
        return json($data);
    }

    //我的邀请订单
    public function invite_order()
    {
        $userid = session('userid');
        $start = 0;
        $limit = 10;
        if (!empty($param['length']) && !empty($param['start'])) {
            $start = ($param['start'] - 1) * $param['length'];
            $limit = $param['length'];
        }
        $child_list = User::where('parent_id', $userid)->field('id')->select();
        $list = array();
        $count = 0;
        foreach ($child_list as $key => $value) {
            $info[] = Shoporder::alias('s')->join('langjin_goods g', 's.goods_id = g.id')->join('langjin_user u', 's.userid = u.id')->where('s.userid', $value['id'])->where('s.status', 1)->field('s.order_id,s.tprice,s.status,s.create_time,g.name,u.id as uid')->limit($start, $limit)->select()->toArray();
            $count = Shoporder::alias('s')->join('langjin_goods g', 's.goods_id = g.id')->join('langjin_user u', 's.userid = u.id')->where('s.userid', $value['id'])->count();
            $count += $count;
        }
        $list = array();
        foreach($info as $value){

            foreach($value as $v){

                $list[]=$v;

            }

        }
        unset($result,$value,$v);
        $data = array('code' => 0, 'msg' => '获取成功', 'info' => $list, 'count' => $count);
        return json($data);
    }
}